
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>python基础 · Aivin开发笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Aivin">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-local-video/video-js.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../styles/website.css">
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="python爬虫.html" />
    
    
    <link rel="prev" href="../" />
    

    
        <link rel="shortcut icon" href='../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="bookmark" href='../assets/images/favicon.ico' type="image/x-icon">
    
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    个人信息
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="python基础.html">
            
                <a href="python基础.html">
            
                    
                    python基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="python爬虫.html">
            
                <a href="python爬虫.html">
            
                    
                    python爬虫
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >python基础</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#python"><b>1. </b>python</a></li><ul><li><span class="title-icon "></span><a href="#python-&#x7F3A;&#x70B9;"><b>1.1. </b>python &#x7F3A;&#x70B9;</a></li><li><span class="title-icon "></span><a href="#python3-&#x6570;&#x636E;&#x7C7B;&#x578B;"><b>1.2. </b>python3 &#x6570;&#x636E;&#x7C7B;&#x578B;</a></li><ul><li><span class="title-icon "></span><a href="#if-&#x5224;&#x65AD;"><b>1.2.1. </b>if &#x5224;&#x65AD;</a></li></ul><li><span class="title-icon "></span><a href="#for-&#x5FAA;&#x73AF;"><b>1.3. </b>for &#x5FAA;&#x73AF;</a></li><li><span class="title-icon "></span><a href="#while-&#x5FAA;&#x73AF;"><b>1.4. </b>while &#x5FAA;&#x73AF;</a></li><li><span class="title-icon "></span><a href="#&#x5217;&#x8868;"><b>1.5. </b>&#x5217;&#x8868;</a></li><ul><li><span class="title-icon "></span><a href="#&#x5143;&#x7EC4;"><b>1.5.1. </b>&#x5143;&#x7EC4;</a></li></ul><li><span class="title-icon "></span><a href="#&#x5B57;&#x5178;"><b>1.6. </b>&#x5B57;&#x5178;</a></li><li><span class="title-icon "></span><a href="#&#x7C7B;&#x3001;&#x5BF9;&#x8C61;"><b>1.7. </b>&#x7C7B;&#x3001;&#x5BF9;&#x8C61;</a></li><li><span class="title-icon "></span><a href="#&#x8FDE;&#x63A5;-mysql"><b>1.8. </b>&#x8FDE;&#x63A5; mysql</a></li><li><span class="title-icon "></span><a href="#&#x53E4;&#x8BD7;&#x6587;&#x7F51;-&#x722C;&#x53D6;&#x4F5C;&#x8005;&#x4FE1;&#x606F;-&#x9759;&#x6001;&#x9875;&#x9762;"><b>1.9. </b>&#x53E4;&#x8BD7;&#x6587;&#x7F51; &#x722C;&#x53D6;&#x4F5C;&#x8005;&#x4FE1;&#x606F; (&#x9759;&#x6001;&#x9875;&#x9762;)</a></li><ul><li><span class="title-icon "></span><a href="#&#x6570;&#x636E;&#x5E93;&#x5E2E;&#x52A9;&#x7C7B;"><b>1.9.1. </b>&#x6570;&#x636E;&#x5E93;&#x5E2E;&#x52A9;&#x7C7B;</a></li><li><span class="title-icon "></span><a href="#&#x4F5C;&#x8005;&#x7C7B;"><b>1.9.2. </b>&#x4F5C;&#x8005;&#x7C7B;</a></li><li><span class="title-icon "></span><a href="#&#x5165;&#x53E3;&#x7A0B;&#x5E8F;"><b>1.9.3. </b>&#x5165;&#x53E3;&#x7A0B;&#x5E8F;</a></li><li><span class="title-icon "></span><a href="#&#x559C;&#x9A6C;&#x62C9;&#x96C5;-json&#x89E3;&#x6790;--&#x6587;&#x4EF6;&#x4E0B;&#x8F7D;"><b>1.9.4. </b>&#x559C;&#x9A6C;&#x62C9;&#x96C5; json&#x89E3;&#x6790; + &#x6587;&#x4EF6;&#x4E0B;&#x8F7D;</a></li><li><span class="title-icon "></span><a href="#&#x873B;&#x8713;fm-&#x722C;&#x866B;"><b>1.9.5. </b>&#x873B;&#x8713;Fm &#x722C;&#x866B;</a></li></ul></ul></ul></div><a href="#python" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="python"><a name="python" class="anchor-navigation-ex-anchor" href="#python"><i class="fa fa-link" aria-hidden="true"></i></a>1. python</h1>
<p>&#x63A8;&#x8350;&#x4F7F;&#x7528; PyCharm &#x8FD9;&#x4E2A;IDE&#x8FDB;&#x884C;&#x4EE3;&#x7801;&#x7F16;&#x5199;&#x3002;</p>
<h2 id="python-&#x7F3A;&#x70B9;"><a name="python-&#x7F3A;&#x70B9;" class="anchor-navigation-ex-anchor" href="#python-&#x7F3A;&#x70B9;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. python &#x7F3A;&#x70B9;</h2>
<pre><code class="lang-xml"> python&#x7684;&#x6E90;&#x7801;&#x4E0D;&#x80FD;&#x52A0;&#x5BC6;&#x3002;
 python&#x662F;&#x89E3;&#x91CA;&#x578B;&#x8BED;&#x8A00;&#xFF0C;&#x8FD0;&#x884C;&#x901F;&#x5EA6;&#x6BD4;java&#x6162;&#x3002;
</code></pre>
<h2 id="python3-&#x6570;&#x636E;&#x7C7B;&#x578B;"><a name="python3-&#x6570;&#x636E;&#x7C7B;&#x578B;" class="anchor-navigation-ex-anchor" href="#python3-&#x6570;&#x636E;&#x7C7B;&#x578B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. python3 &#x6570;&#x636E;&#x7C7B;&#x578B;</h2>
<p>Python&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x4E0D;&#x9700;&#x8981;&#x58F0;&#x660E;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-number">1</span>&#x3001;&#x5B57;&#x7B26;&#x4E32;
<span class="hljs-number">2</span>&#x3001;&#x6570;&#x5B57;
<span class="hljs-number">3</span>&#x3001;&#x5217;&#x8868;
<span class="hljs-number">4</span>&#x3001;&#x5143;&#x7EC4;
<span class="hljs-number">5</span>&#x3001;&#x96C6;&#x5408;
<span class="hljs-number">6</span>&#x3001;&#x5B57;&#x5178;
<span class="hljs-number">7</span>&#x3001;&#x65E5;&#x671F;
</code></pre>
<h3 id="if-&#x5224;&#x65AD;"><a name="if-&#x5224;&#x65AD;" class="anchor-navigation-ex-anchor" href="#if-&#x5224;&#x65AD;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.1. if &#x5224;&#x65AD;</h3>
<pre><code class="lang-python"> age = <span class="hljs-number">20</span>
 <span class="hljs-keyword">if</span> age &gt; <span class="hljs-number">18</span>:
     print(<span class="hljs-string">&apos;aaa&apos;</span>)
 <span class="hljs-keyword">elif</span> age &gt;= <span class="hljs-number">6</span>:
     print(<span class="hljs-string">&apos;bbb&apos;</span>)
 <span class="hljs-keyword">else</span>:
     print(<span class="hljs-string">&apos;ccc&apos;</span>)
</code></pre>
<h2 id="for-&#x5FAA;&#x73AF;"><a name="for-&#x5FAA;&#x73AF;" class="anchor-navigation-ex-anchor" href="#for-&#x5FAA;&#x73AF;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. for &#x5FAA;&#x73AF;</h2>
<pre><code class="lang-python">list = [<span class="hljs-number">100</span>, <span class="hljs-number">200</span>, <span class="hljs-number">300</span>]
<span class="hljs-keyword">for</span> temp <span class="hljs-keyword">in</span> list:
   print(temp)
</code></pre>
<h2 id="while-&#x5FAA;&#x73AF;"><a name="while-&#x5FAA;&#x73AF;" class="anchor-navigation-ex-anchor" href="#while-&#x5FAA;&#x73AF;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. while &#x5FAA;&#x73AF;</h2>
<pre><code class="lang-python">N = <span class="hljs-number">10</span>
x = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> x &lt; N:
   x = x + <span class="hljs-number">1</span>
   print(x)
   <span class="hljs-keyword">if</span> x &gt; <span class="hljs-number">6</span>:
       <span class="hljs-keyword">break</span>
</code></pre>
<h2 id="&#x5217;&#x8868;"><a name="&#x5217;&#x8868;" class="anchor-navigation-ex-anchor" href="#&#x5217;&#x8868;"><i class="fa fa-link" aria-hidden="true"></i></a>1.5. &#x5217;&#x8868;</h2>
<pre><code class="lang-python"><span class="hljs-comment"># &#x96C6;&#x5408;&#x4E2D;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x4E0D;&#x4E00;&#x6837;&#x7684;</span>
mList = [<span class="hljs-string">&apos;Michael&apos;</span>, <span class="hljs-number">100</span>, <span class="hljs-keyword">True</span>]
print(<span class="hljs-string">&quot;&#x7B2C;&#x4E00;&#x4E2A;&#x6570;&#x636E;=&quot;</span> + mList[<span class="hljs-number">0</span>])
print(<span class="hljs-string">&quot;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x6570;&#x636E;=&quot;</span> + str(mList[<span class="hljs-number">-1</span>]))
print(<span class="hljs-string">&quot;&#x6570;&#x7EC4;&#x5927;&#x5C0F;=&quot;</span> + str(len(mList)))
mList.append(<span class="hljs-string">&apos;AA&apos;</span>)
mList.insert(<span class="hljs-number">0</span>, <span class="hljs-string">&apos;BB&apos;</span>)
<span class="hljs-comment"># &#x5220;&#x9664;&#x6700;&#x540E;&#x4E00;&#x4E2A;</span>
mList.pop()
<span class="hljs-comment"># &#x5220;&#x9664;&#x6307;&#x5B9A;&#x7D22;&#x5F15;&#x7684;&#x6570;&#x636E;</span>
mList.pop(<span class="hljs-number">1</span>)
</code></pre>
<h3 id="&#x5143;&#x7EC4;"><a name="&#x5143;&#x7EC4;" class="anchor-navigation-ex-anchor" href="#&#x5143;&#x7EC4;"><i class="fa fa-link" aria-hidden="true"></i></a>1.5.1. &#x5143;&#x7EC4;</h3>
<p>&#x7C7B;&#x4F3C;java&#x4E2D;&#x7684;&#x6570;&#x7EC4;</p>
<pre><code class="lang-python">t = (<span class="hljs-string">&apos;AA&apos;</span>, <span class="hljs-number">1</span> , <span class="hljs-keyword">True</span>)
<span class="hljs-keyword">print</span> (t)
</code></pre>
<h2 id="&#x5B57;&#x5178;"><a name="&#x5B57;&#x5178;" class="anchor-navigation-ex-anchor" href="#&#x5B57;&#x5178;"><i class="fa fa-link" aria-hidden="true"></i></a>1.6. &#x5B57;&#x5178;</h2>
<pre><code class="lang-python">dict = {
   <span class="hljs-string">&apos;Adam&apos;</span>: <span class="hljs-number">95</span>,
   <span class="hljs-string">&apos;Lisa&apos;</span>: <span class="hljs-string">&quot;aaaaaaa&quot;</span>,
   <span class="hljs-string">&apos;Bart&apos;</span>: <span class="hljs-keyword">True</span>
}

<span class="hljs-comment"># &#x6839;&#x636E; key &#x83B7;&#x5F97; &#x503C;</span>
<span class="hljs-keyword">for</span> mkey <span class="hljs-keyword">in</span> dict:
   print(dict[mkey])

<span class="hljs-comment"># &#x83B7;&#x5F97;&#x5168;&#x90E8;&#x7684;&#x503C;</span>
<span class="hljs-keyword">for</span> temp <span class="hljs-keyword">in</span> dict.values():
   print(temp)

<span class="hljs-comment"># &#x53D6;&#x51FA;&#x952E;&#x503C;&#x5BF9;</span>
<span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> dict.items():
   print(key, <span class="hljs-string">&apos; &#xFF0C;&apos;</span>, value)
</code></pre>
<h2 id="&#x7C7B;&#x3001;&#x5BF9;&#x8C61;"><a name="&#x7C7B;&#x3001;&#x5BF9;&#x8C61;" class="anchor-navigation-ex-anchor" href="#&#x7C7B;&#x3001;&#x5BF9;&#x8C61;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7. &#x7C7B;&#x3001;&#x5BF9;&#x8C61;</h2>
<pre><code class="lang-xml"># &#x7EE7;&#x627F;&#x81EA; object
class Person3(object):
   # &#x7C7B;&#x5C5E;&#x6027;
   address=&apos;&#x5730;&#x7403;&apos;

   # &#x6784;&#x9020;&#x65B9;&#x6CD5;
   def __init__(self, name):
       # &#x5BF9;&#x8C61;&#x5C5E;&#x6027;
       # &#x516C;&#x5F00;
       self.name = name
       # &#x516C;&#x5F00;
       self._sex = &apos;&#x5973;&apos;
       #&#x79C1;&#x6709;
       self.__job = &apos;&#x5B66;&#x751F;&apos;#&#x5916;&#x90E8;&#x4E0D;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;

   # &#x7C7B;&#x65B9;&#x6CD5;
   @classmethod
   def getAddress(self):
       return self.address

   # &#x5BF9;&#x8C61;&#x65B9;&#x6CD5;
   def printInfo(self):
       print(&quot;&#x5F53;&#x524D;&#x5BF9;&#x8C61;&#x540D;&#x5B57;&#x662F;&#xFF1A;&quot;, self.name)


# &#x8BBE;&#x7F6E;&#x3001;&#x83B7;&#x53D6;&#x5C5E;&#x6027;
p= Person3(&apos;&#x5C0F;&#x82B3;&apos;)
setattr(p, &apos;name&apos;, &apos;small dog&apos;)
msg = getattr(p, &apos;name&apos;)

# &#x5BF9;&#x8C61;&#x7C7B;&#x578B;&#x5224;&#x65AD;
if isinstance(p,object):
   print(&apos;&#x7C7B;&#x578B;&#x5224;&#x65AD;&#x6B63;&#x786E;&apos;)
else:
   print(&apos;&#x7C7B;&#x578B;&#x5224;&#x65AD;&#x9519;&#x8BEF;&apos;)
</code></pre>
<h2 id="&#x8FDE;&#x63A5;-mysql"><a name="&#x8FDE;&#x63A5;-mysql" class="anchor-navigation-ex-anchor" href="#&#x8FDE;&#x63A5;-mysql"><i class="fa fa-link" aria-hidden="true"></i></a>1.8. &#x8FDE;&#x63A5; mysql</h2>
<p>&#x5B89;&#x88C5; pymysql &#x63D2;&#x4EF6;&#x3002; &#x5982;&#x679C;&#x4F7F;&#x7528;&#x7684;&#x662F;pycharm&#xFF0C; &#x662F;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;python&#x73AF;&#x5883;&#x76EE;&#x5F55;&#x7684;&#x3002;&#x65B9;&#x4FBF;&#x7075;&#x6D3B;&#x5207;&#x6362;2.x&#x548C;3.x .</p>
<pre><code class="lang-xml">import pymysql
from poetry.author import Author

conn= pymysql.connect( host=&apos;114.xx.xx.65&apos;, port=3306, user=&apos;xx&apos;, passwd=&apos;xx&apos;,db=&apos;xx&apos;,charset=&apos;UTF8&apos;)
print(&quot;&#x83B7;&#x5F97;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;:&quot;)
print(conn)
cursor = conn.cursor()

def addAuthor(author):
    sql = &apos; insert  into poetryauthor( mname, msex , mdesc, mpic,mtime,mtype ) values(  &quot;%s&quot; ,&quot;%s&quot; ,&quot;%s&quot; ,&quot;%s&quot; ,&quot;%s&quot; ,&quot;%s&quot; ) &apos;\
         % (author.mname,  author.msex, author.mdesc, author.mpic, author.mtime, author.mtype)
    cursor.execute( sql)
    newId = int(conn.insert_id())
    conn.commit()
    print(&quot;&#x63D2;&#x5165; &#x65B0;&#x8BB0;&#x5F55;&#x7684;id&#x662F;:  &quot;, newId)


def deleteAuthorById( mId):
    sql = &apos;delete from poetryauthor where mId = %d  &apos;% (mId)
    count = cursor.execute(sql)
    conn.commit()
    print(&quot;&#x5220;&#x9664;&#x7ED3;&#x679C;&#xFF1A;&quot;, count)

def updateAutorById(author ,mId):
    sql = &apos;update poetryauthor set mname=&quot;%s&quot;, msex=&quot;%s&quot;, mdesc=&quot;%s&quot;, mpic=&quot;%s&quot;, mtime=&quot;%s&quot; , mtype=&quot;%s&quot; where mId=%d &apos; \
          % (author.mname, author.msex, author.mdesc, author.mpic, author.mtime, author.mtype, mId)
    count = cursor.execute(sql)
    print(&quot;&#x66F4;&#x65B0;&#x7ED3;&#x679C;&#xFF1A;&quot;, count)
    conn.commit()


def queryAuthor():
    count=cursor.execute(&apos;select * from poetryauthor&apos;)
    print(&apos;&#x5171;&#x6709;&apos;+str(count)+&apos; &#x6761;&#x8BB0;&#x5F55;&apos;)
    rows = cursor.fetchall()
    for row in rows:
        mid   = row[0]
        mname = row[1]
        msex  = row[2]
        mdesc = row[3]
        mpic  = row[4]
        mtime = row[5]
        mtype = row[6]
        print(mid, mname, msex , mdesc, mpic,mtime,mtype)

# &#x6D4B;&#x8BD5;
author = Author(&quot;&#x674E;&#x767D;2&quot;,  &quot;&#x7537;1&quot; ,  &quot;&#x725B;&#x903C;&#x8BD7;&#x4EBA;1&quot;,  &quot;baidu.com...png1&quot;,   &quot;&#x5510;&#x4EE3;1&quot;  , &quot;&#x6D6A;&#x6F2B;&#x4E3B;&#x4E49;1&quot;)
# addAuthor(author)
# queryAuthor()
# deleteAuthorById(5)
# updateAutorById(author , 6)
# queryAuthor()

# &#x5173;&#x95ED;&#x8FDE;&#x63A5;&#x7B49;...
cursor.close()
conn.commit()
conn.close()
</code></pre>
<h2 id="&#x53E4;&#x8BD7;&#x6587;&#x7F51;-&#x722C;&#x53D6;&#x4F5C;&#x8005;&#x4FE1;&#x606F;-&#x9759;&#x6001;&#x9875;&#x9762;"><a name="&#x53E4;&#x8BD7;&#x6587;&#x7F51;-&#x722C;&#x53D6;&#x4F5C;&#x8005;&#x4FE1;&#x606F;-&#x9759;&#x6001;&#x9875;&#x9762;" class="anchor-navigation-ex-anchor" href="#&#x53E4;&#x8BD7;&#x6587;&#x7F51;-&#x722C;&#x53D6;&#x4F5C;&#x8005;&#x4FE1;&#x606F;-&#x9759;&#x6001;&#x9875;&#x9762;"><i class="fa fa-link" aria-hidden="true"></i></a>1.9. &#x53E4;&#x8BD7;&#x6587;&#x7F51; &#x722C;&#x53D6;&#x4F5C;&#x8005;&#x4FE1;&#x606F; (&#x9759;&#x6001;&#x9875;&#x9762;)</h2>
<h3 id="&#x6570;&#x636E;&#x5E93;&#x5E2E;&#x52A9;&#x7C7B;"><a name="&#x6570;&#x636E;&#x5E93;&#x5E2E;&#x52A9;&#x7C7B;" class="anchor-navigation-ex-anchor" href="#&#x6570;&#x636E;&#x5E93;&#x5E2E;&#x52A9;&#x7C7B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.9.1. &#x6570;&#x636E;&#x5E93;&#x5E2E;&#x52A9;&#x7C7B;</h3>
<pre><code class="lang-xml">class MySqlTool(object):
    def __init__(self):
        self.conn = pymysql.connect(host=&apos;xx.xx.xx.xx65&apos;, port=3306, user=&apos;xx&apos;, passwd=&apos;xx&apos;,
                                    db=&apos;xx&apos;, charset=&apos;UTF8&apos;)
        print(&quot;&#x83B7;&#x5F97;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;:&quot;)
        print(self.conn)
        self.cursor = self.conn.cursor()

    def addAuthor(self ,author):
        sql = &quot; insert  into poetryauthor( mname, msex , mdesc, mpic,mtime,mtype ) values(  &apos;%s&apos;,&apos;%s&apos;,&apos;%s&apos;,&apos;%s&apos;,&apos;%s&apos;,&apos;%s&apos;  ) &quot; \
              % (author.mname, author.msex, author.mdesc, author.mpic, author.mtime, author.mtype)
        self.cursor.execute(sql)
        newId = int(self.conn.insert_id())
        self.conn.commit()
        print(&quot;&#x63D2;&#x5165; &#x65B0;&#x8BB0;&#x5F55;&#x7684;id&#x662F;:  &quot;, newId)

    def deleteAuthorById(self, mId):
        sql = &apos;delete from poetryauthor where mId = %d  &apos; % (mId)
        count = self.cursor.execute(sql)
        self.conn.commit()
        print(&quot;&#x5220;&#x9664;&#x7ED3;&#x679C;&#xFF1A;&quot;, count)

    def updateAutorById(self, author, mId):
        sql = &quot; update poetryauthor set mname= &apos;%s&apos;, msex=&apos;%s&apos;, mdesc=&apos;%s&apos;, mpic=&apos;%s&apos;, mtime=&apos;%s&apos; , mtype=&apos;%s&apos; where mId=%d &quot; \
              % (author.mname, author.msex, author.mdesc, author.mpic, author.mtime, author.mtype, mId)
        count = self.cursor.execute(sql)
        print(&quot;&#x66F4;&#x65B0;&#x7ED3;&#x679C;&#xFF1A;&quot;, count)
        self.conn.commit()

    def queryAuthor(self):
        count = self.cursor.execute(&apos;select * from poetryauthor&apos;)
        print(&apos;&#x5171;&#x6709;&apos; + str(count) + &apos; &#x6761;&#x8BB0;&#x5F55;&apos;)
        rows = self.cursor.fetchall()
        for row in rows:
            mid = row[0]
            mname = row[1]
            msex = row[2]
            mdesc = row[3]
            mpic = row[4]
            mtime = row[5]
            mtype = row[6]
            print(mid, mname, msex, mdesc, mpic, mtime, mtype)

    def closeConnection(self):
        # &#x5173;&#x95ED;&#x8FDE;&#x63A5;&#x7B49;...
        self.cursor.close()
        self.conn.commit()
        self.conn.close()
</code></pre>
<h3 id="&#x4F5C;&#x8005;&#x7C7B;"><a name="&#x4F5C;&#x8005;&#x7C7B;" class="anchor-navigation-ex-anchor" href="#&#x4F5C;&#x8005;&#x7C7B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.9.2. &#x4F5C;&#x8005;&#x7C7B;</h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Author</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-comment"># &#x6784;&#x9020;&#x65B9;&#x6CD5;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self ,mname, msex , mdesc, mpic,mtime,mtype)</span>:</span>
        self.mname = mname
        self.msex = msex
        self.mdesc = mdesc
        self.mpic = mpic
        self.mtime = mtime
        self.mtype = mtype

    <span class="hljs-comment"># &#x5BF9;&#x8C61;&#x65B9;&#x6CD5;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">printInfo</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;&#x5F53;&#x524D;&#x5BF9;&#x8C61;&#x4FE1;&#x606F;&#xFF1A;&quot;</span>,  self.mname, self.msex, self.mtime, self.mtype, self.mpic, self.mdesc)
</code></pre>
<h3 id="&#x5165;&#x53E3;&#x7A0B;&#x5E8F;"><a name="&#x5165;&#x53E3;&#x7A0B;&#x5E8F;" class="anchor-navigation-ex-anchor" href="#&#x5165;&#x53E3;&#x7A0B;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.9.3. &#x5165;&#x53E3;&#x7A0B;&#x5E8F;</h3>
<pre><code class="lang-python">user_agent = <span class="hljs-string">&apos;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)&apos;</span>
headers = {<span class="hljs-string">&apos;User-Agent&apos;</span>: user_agent}
session = requests.session()
sqlTool = MySqlTool()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getAsleep</span><span class="hljs-params">()</span>:</span>
    sleepTime = random.uniform(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>)
    print(<span class="hljs-string">&quot;&#x4F11;&#x7720;&#xFF1A;&quot;</span>, sleepTime)
    time.sleep(sleepTime)


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getUrlData</span><span class="hljs-params">( url ,mTime)</span>:</span>
    <span class="hljs-comment"># print(&quot;--------------------------------------&quot;)</span>
    <span class="hljs-comment"># print(url)</span>
    page = session.get(url, headers=headers)
    soup = BeautifulSoup(page.text, <span class="hljs-string">&apos;lxml&apos;</span>)
    <span class="hljs-comment"># contents = soup.findAll(&apos;div&apos;, class=&apos;left&apos;)</span>
    datas = soup.findAll(name=<span class="hljs-string">&apos;div&apos;</span>, attrs={<span class="hljs-string">&quot;class&quot;</span>: <span class="hljs-string">&quot;left&quot;</span>})
    <span class="hljs-keyword">for</span> data <span class="hljs-keyword">in</span> datas:
        items  = data.findAll( name= <span class="hljs-string">&apos;div&apos;</span>,attrs={<span class="hljs-string">&quot;class&quot;</span>: <span class="hljs-string">&quot;sonspic&quot;</span>})
        <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> items:
            urlDiv  =  item.find( name= <span class="hljs-string">&apos;div&apos;</span>,attrs={<span class="hljs-string">&quot;class&quot;</span>: <span class="hljs-string">&quot;divimg&quot;</span>})
            <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> urlDiv <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:
                imgUrl= urlDiv.find(name= <span class="hljs-string">&apos;img&apos;</span>)
                name= imgUrl[<span class="hljs-string">&apos;alt&apos;</span>]
                pic= imgUrl[<span class="hljs-string">&apos;src&apos;</span>]
                descP =  item.find( name= <span class="hljs-string">&apos;p&apos;</span>,attrs={<span class="hljs-string">&quot;style&quot;</span>: <span class="hljs-string">&quot; margin:0px;&quot;</span>})
                desc= descP.text
                pos = desc.index(<span class="hljs-string">&quot;&#x25BA;&quot;</span>)  <span class="hljs-comment"># &#x53BB;&#x6389;&#x65E0;&#x7528;&#x7684;&#x5185;&#x5BB9;</span>
                desc = desc[<span class="hljs-number">0</span>:pos]

                author = Author(name, <span class="hljs-string">&quot;&quot;</span>, desc, pic, mTime, <span class="hljs-string">&quot;&quot;</span>)
                author.printInfo()
                sqlTool.addAuthor(author)
                print(<span class="hljs-string">&quot;--------------------------------------&quot;</span>)


dict = {
    <span class="hljs-comment"># &quot;&#x5148;&#x79E6;&quot;: 4,</span>
    <span class="hljs-comment"># &quot;&#x4E24;&#x6C49;&quot;: 10,</span>
    <span class="hljs-string">&quot;&#x9B4F;&#x664B;&quot;</span>: <span class="hljs-number">13</span>,
    <span class="hljs-string">&quot;&#x5357;&#x5317;&#x671D;&quot;</span>: <span class="hljs-number">20</span>,
    <span class="hljs-string">&quot;&#x968B;&#x4EE3;&quot;</span>: <span class="hljs-number">3</span>,
    <span class="hljs-string">&quot;&#x5510;&#x4EE3;&quot;</span>: <span class="hljs-number">232</span>,
    <span class="hljs-string">&quot;&#x4E94;&#x4EE3;&quot;</span>: <span class="hljs-number">4</span>,
    <span class="hljs-string">&quot;&#x5B8B;&#x4EE3;&quot;</span>: <span class="hljs-number">583</span>,
    <span class="hljs-string">&quot;&#x91D1;&#x671D;&quot;</span>: <span class="hljs-number">10</span>,
    <span class="hljs-string">&quot;&#x5143;&#x4EE3;&quot;</span>: <span class="hljs-number">64</span>,
    <span class="hljs-string">&quot;&#x660E;&#x4EE3;&quot;</span>: <span class="hljs-number">279</span>,
    <span class="hljs-string">&quot;&#x6E05;&#x4EE3;&quot;</span>: <span class="hljs-number">598</span>
}


<span class="hljs-keyword">for</span> mkey <span class="hljs-keyword">in</span> dict:
    index = <span class="hljs-number">0</span>
    sum = dict[mkey]
    <span class="hljs-keyword">while</span> index &lt;= sum:
        url = <span class="hljs-string">&quot;https://so.gushiwen.org/authors/Default.aspx?p=%d&amp;c=%s&quot;</span> % (index, mkey)
        getUrlData(url, mkey)
        index = index + <span class="hljs-number">1</span>
        getAsleep()

sqlTool.closeConnection()
</code></pre>
<h3 id="&#x559C;&#x9A6C;&#x62C9;&#x96C5;-json&#x89E3;&#x6790;--&#x6587;&#x4EF6;&#x4E0B;&#x8F7D;"><a name="&#x559C;&#x9A6C;&#x62C9;&#x96C5;-json&#x89E3;&#x6790;--&#x6587;&#x4EF6;&#x4E0B;&#x8F7D;" class="anchor-navigation-ex-anchor" href="#&#x559C;&#x9A6C;&#x62C9;&#x96C5;-json&#x89E3;&#x6790;--&#x6587;&#x4EF6;&#x4E0B;&#x8F7D;"><i class="fa fa-link" aria-hidden="true"></i></a>1.9.4. &#x559C;&#x9A6C;&#x62C9;&#x96C5; json&#x89E3;&#x6790; + &#x6587;&#x4EF6;&#x4E0B;&#x8F7D;</h3>
<pre><code>import requests
import urllib.request
import demjson

user_agent = &apos;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)&apos;
headers = {&apos;User-Agent&apos;: user_agent}
session = requests.session()

def getAsleep():
    sleepTime = random.uniform(0, 2)
    print(&quot;&#x4F11;&#x7720;&#xFF1A;&quot;, sleepTime)
    time.sleep(sleepTime)

def downAFile(fileName ,fileUrl ,downCallBack):
    downPath = &apos;F:\\&#x5C0F;&#x4F0A;\\%s.m4a&apos;%(fileName)
    print(&quot;&#x6B63;&#x5728;&#x4E0B;&#x8F7D;&#xFF1A;&quot; ,fileName)
    urllib.request.urlretrieve(fileUrl, downPath ,downCallBack)

def downCallBack(downedCount, countSize, allSize):
    per = 100.0 * downedCount * countSize / allSize
    if per &gt; 100:
        per = 100
    print(&apos;&#x4E0B;&#x8F7D;&#x8FDB;&#x5EA6;&#xFF1A;  %.2f%%&apos; % per)


def getUrlData( url ):
    page = session.get(url, headers=headers)
    jsonData = page.text

    bean1 = demjson.decode(jsonData)
    bean2 = bean1[&apos;data&apos;]
    bean3 = bean2[&apos;tracksAudioPlay&apos;]
    for data in bean3:
     name = data[&apos;trackName&apos;]
     # print(&quot;&#x540D;&#x5B57;&#xFF1A;&quot; ,name)

     arrays= name.split(&quot;-&quot;)
     if len(arrays)&lt; 2:
         arrays = name.split(&quot; &quot;)
     name = arrays[len(arrays)-1]
     src = data[&apos;src&apos;]
     downAFile(name ,src ,downCallBack)

urls = [
&quot;https://www.ximalaya.com/revision/play/album?albumId=10564571&amp;pageNum=1&amp;sort=-1&amp;pageSize=300&quot; ,
&quot;https://www.ximalaya.com/revision/play/album?albumId=10564571&amp;pageNum=2&amp;sort=-1&amp;pageSize=300&quot;,
&quot;https://www.ximalaya.com/revision/play/album?albumId=10564571&amp;pageNum=3&amp;sort=-1&amp;pageSize=300&quot;,
&quot;https://www.ximalaya.com/revision/play/album?albumId=10564571&amp;pageNum=4&amp;sort=-1&amp;pageSize=300&quot;,
&quot;https://www.ximalaya.com/revision/play/album?albumId=10564571&amp;pageNum=5&amp;sort=-1&amp;pageSize=300&quot;
]
for url in urls:
    getUrlData(url)
    getAsleep()
</code></pre><h3 id="&#x873B;&#x8713;fm-&#x722C;&#x866B;"><a name="&#x873B;&#x8713;fm-&#x722C;&#x866B;" class="anchor-navigation-ex-anchor" href="#&#x873B;&#x8713;fm-&#x722C;&#x866B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.9.5. &#x873B;&#x8713;Fm &#x722C;&#x866B;</h3>
<p>&#x722C;&#x53D6;&#x7F51;&#x6613;&#x8F7B;&#x677E;&#x4E00;&#x523B;
<code>xml</code></p>
<footer class="page-footer"><span class="copyright">&#x65E0;&#x7248;&#x6743; &#x53EF;&#x4EE5;&#x65E0;&#x9650;&#x5236;&#x4F7F;&#x7528;&#xFF01; all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2019-06-18 16:14:16
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../" class="navigation navigation-prev " aria-label="Previous page: 个人信息">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="python爬虫.html" class="navigation navigation-next " aria-label="Next page: python爬虫">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"python基础","level":"1.2","depth":1,"next":{"title":"python爬虫","level":"1.3","depth":1,"path":"chinese/python爬虫.md","ref":"chinese/python爬虫.md","articles":[]},"previous":{"title":"个人信息","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","search-plus","todo","anchor-navigation-ex","copy-code-button","fontsettings","tbfed-pagefooter","donate","local-video","splitter","favicon","theme-comscore","local-video"],"styles":{"website":"styles/website.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"无版权 可以无限制使用！","modify_label":"该文件更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"todo":{},"splitter":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"donate":{"alipay":"http://oyru0ayig.bkt.clouddn.com/201803011956_osChina_支付宝收款码.png","alipayText":"支付宝打赏","button":"打赏","title":"","wechat":"http://oyru0ayig.bkt.clouddn.com/201803011956_osChina_微信收款码.png","wechatText":"微信打赏"},"fontsettings":{"family":"sans","size":2,"theme":"sepia"},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":{"shortcut":"assets/images/favicon.ico","bookmark":"assets/images/favicon.ico"},"theme-comscore":{},"local-video":{},"copy-code-button":{},"sharing":{"all":["facebook","google","twitter","weibo","instapaper"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","author":"Aivin","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Aivin开发笔记","language":"zh-hans","gitbook":"*","description":"Aivin开发笔记"},"file":{"path":"chinese/python基础.md","mtime":"2019-06-18T08:14:16.957Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-10T03:42:00.734Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-local-video/video.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

